import { combineReducers,AnyAction } from "redux";
import * as actionTypes from './constance'
import { video } from "@/models/videoType";
const initialState={
    homeRecommend:{
        loading:false,
        recommendEntrance:[],
        recommendCourse:[],
        seletorContentTabs:[],
        recommendVideos:[]
    },
    Search:{
        loading:false,
        hotWord:[],
        hotCourse:[],
        suggestionList:[]
    }
}
const recommendReducer=(state=initialState.homeRecommend,action:AnyAction)=>{
    switch(action.type){
        case actionTypes.SET_RECOMMEND_ENTRANCE:
            return{
                ...state,
                recommendEntrance:action.data
            }
        case actionTypes.SET_RECOMMEND_COURSE:
            return{
                ...state,
                recommendCourse:action.data
            }
        case actionTypes.SET_CONTENT_TABS:
            return{
                ...state,
                seletorContentTabs:action.data
            }
        case actionTypes.SET_RECOMMEND_VIDEOS:
            return{
                ...state,
                recommendVideos:action.data
            }
        case actionTypes.SET_RECOMMEND_LOADING:
            return{
                ...state,
                loading:action.data
            }
        case actionTypes.SET_VIDEO_ISFAVORITES:
            let newrecommendVideos=state.recommendVideos.map((item:any)=>{
                if(item['index'] === action.index){
                    item['videoProcessingCard']['baseInfo']['favorites']
                    =!item['videoProcessingCard']['baseInfo']['favorites']
                }
                return item
            })
            return{
                ...state,
                recommendVideos:[...newrecommendVideos]    
            }
        
        default:
            return state;
    }
}

const searchReducer=(state=initialState.Search,action:AnyAction)=>{
    switch(action.type){
        case actionTypes.SET_SEARCH_LOADING:
            return{
                ...state,
                loading:action.data
            }
        case actionTypes.SET_HOTWORD:
            return{
                ...state,
                hotWord:action.data
            }
        case actionTypes.SET_HOTCOURSE:
            return {
                ...state,
                hotCourse:action.data
            }
        case actionTypes.SET_SUGGESTION:
            return{
                ...state,
                suggestionList:action.data
            }
        default:
            return state
    }
}

export default combineReducers({
    recommendData:recommendReducer,
    searchData:searchReducer
})